package com.shanghaionstar.user;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.onstar.cn.common.exception.OnStarException;
import com.onstar.cn.common.log.LogManager;
import com.onstar.cn.framework.annotation.FindViewById;
import com.onstar.cn.framework.mvp.base.core.BaseFragment;
import com.onstar.cn.more.entity.CardHistories;
import com.onstar.cn.more.response.GetCardHistoryResponse;
import com.shanghaionstar.R;
import com.shanghaionstar.user.adapter.HistoryAdapter;
import com.shanghaionstar.user.iview.IActivieHistiryView;
import com.shanghaionstar.user.presenter.ActiveHistoryPresenter;
import com.shanghaionstar.weight.TopLayout;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Mengzhou Gu on 2016/8/3.
 */
public class ActiviteHistoryFragment extends BaseFragment implements AbsListView.OnScrollListener, IActivieHistiryView {
    @FindViewById
    private TopLayout top_layout = null;
    private PrePayActivity parentActivity = null;
    @FindViewById
    private ListView lv_history_activate = null;
    private boolean isExtend = false;
    private ProgressDialog dialog;
    private static LogManager logger = LogManager.getInstance();
    private Bundle bundle = null;
    private String vin = null;
    private String card_number = null;
    private List<CardHistories> alldata = new ArrayList<CardHistories>();
    private View loadmore = null;
    private LinearLayout footview = null;
    private int currentPage = 0;// 当前页,默认为1
    private int MaxDateNum = 0;
    private HistoryAdapter historyAdapter = null;
    private String from;
    @FindViewById
    private TextView no_datas;
    private ActiveHistoryPresenter presenter;

    @Override
    public int getLayoutId() {
        return R.layout.user_active_history_fragment;
    }


    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {

    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
        if (totalItemCount < 5) {
            lv_history_activate.removeFooterView(loadmore);
        }
        if (totalItemCount > 0) {
            if (totalItemCount > MaxDateNum) {
                lv_history_activate.removeFooterView(loadmore);
            }
        }
    }

    public void onResume() {
        super.onResume();

    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        parentActivity = (PrePayActivity) getActivity();
        parentActivity.current_fragment = "ActiviteHistoryFragment";
        bundle = getArguments();

        presenter = new ActiveHistoryPresenter(parentActivity.getScheduler(), parentActivity);
        presenter.setView(this);
        if (bundle != null) {
            vin = bundle.getString("vin");
            card_number = bundle.getString("card_number");
            from = bundle.getString("from");
        }
        //  getCardHistory(currentPage);
        presenter.getData(currentPage);

        lv_history_activate.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                historyAdapter.setSelectItem(arg2);
                historyAdapter.notifyDataSetChanged();

            }
        });

        top_layout.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                if (!TextUtils.isEmpty(from) && from.equalsIgnoreCase(PayCardDetailFragment.class.getSimpleName())) {
                    PayCardFragment.backFromActiveHis = true;
                    parentActivity.removeFragmentsUntilOne(parentActivity.fragmentManager, PayCardDetailFragment.class, true);
                    back();
                } else {
                    back();
                }
            }
        });
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        top_layout = null;
    }

    @Override
    public void showSuccess(GetCardHistoryResponse getCardHistoryResponse) {
        if (getCardHistoryResponse != null) {
            // 最大条数
            MaxDateNum = getCardHistoryResponse.getPageable().getTotalSize();
            if (MaxDateNum <= 0) {
                no_datas.setText(getString(R.string.no_ppc_record));
                no_datas.setVisibility(View.VISIBLE);
                lv_history_activate.setVisibility(View.GONE);
            } else {
                no_datas.setVisibility(View.GONE);
                lv_history_activate.setVisibility(View.VISIBLE);
            }
            if (getCardHistoryResponse.getOrderList() != null) {
                // 绑定数据
                ArrayList<CardHistories> current_data = (ArrayList<CardHistories>) getCardHistoryResponse.getOrderList();
                if (alldata != null) {
                    alldata.clear();
                }
                alldata = new ArrayList<CardHistories>();
                alldata.addAll(current_data);
                if (historyAdapter == null) {
                    historyAdapter = new HistoryAdapter(alldata, getActivity());
                    lv_history_activate.setAdapter(historyAdapter);
                } else {
                    historyAdapter.notifyDataSetChanged();
                }

                // 添加底部加载视图
                //								if (currentPage + 5 < MaxDateNum) {
                //									more_layout.setVisibility(View.VISIBLE);
                //									tv_more.setVisibility(View.VISIBLE);
                //									more_progress.setVisibility(View.GONE);
                //								} else {
                //									more_layout.setVisibility(View.GONE);
                //								}
            }
        }
    }

    @Override
    public void showFailure() {

    }

    @Override
    public void showError(Throwable throwable) {
        // dismissDialog(dialog);
        no_datas.setText(getString(R.string.error_ppc_record));
        no_datas.setVisibility(View.VISIBLE);
        lv_history_activate.setVisibility(View.GONE);
    }

    @Override
    public void setLoading(boolean isLoading) {
        if (isLoading) {
            dialog = showLoadProgress();
            dialog.setCancelable(false);//http://10.16.138.85:8080
            dialog.setCanceledOnTouchOutside(false);
            dialog.show();
        } else {
            dismissDialog(dialog);
        }
    }

    @Override
    public void onResponseError(OnStarException e) {

    }

    @Override
    public void onReponseSuccess(GetCardHistoryResponse object) {

    }

    @Override
    public Activity onActivity() {
        return getActivity();
    }
}
